package com.inossem.warehousestorage_inossem.activity.inventory.chuangyou;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inventory.chuangyou.InventorySearchCompanyAdapter;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventorySearchCompanyBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.chuangyou.InventorySearchCompanyRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/2 16:48
 * @email jianyu.zhan@inossem.com
 */
public class InventorySearchCompanyActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private PullableListView listView;

    private String mCondition;
    private List<InventorySearchCompanyBean.BodyBean> allData;
    private InventorySearchCompanyAdapter adapter;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_inventory_listview);

        initView(this);
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) findViewById(R.id.headerTitle);
        listView = (PullableListView) findViewById(R.id.listView);

        listView.setPullDownFlag(false);
        listView.setPullUpFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("搜索");

        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        resetData(activity);
    }

    private void resetData(Activity context) {
        mCondition = "";
        allData = new ArrayList<>();
        addListData(context, allData, listView, true);
    }

    private void search(Activity activity, String condition) {
        InventorySearchCompanyRequest.request(activity, condition, new GsonBeanListener<InventorySearchCompanyBean>() {
            @Override
            public void onSuccess(InventorySearchCompanyBean bean) {
                allData = bean.getBody();
                if (allData.isEmpty()) {
                    Utils.middleToast(activity, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addListData(activity, ActivityUtils.getShowData(allData, 0), listView, true);
            }
        });
    }

    private void addListData(Activity context, List<InventorySearchCompanyBean.BodyBean> list, ListView listView, boolean flag) {
        adapter = new InventorySearchCompanyAdapter(context, list, mCondition, new SearchListener() {
            @Override
            public NoDoubleClickListener getNoDoubleClickListener(EditText condition) {
                NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        Utils.closeSoftKeyboard(context, v);
                        mCondition = condition.getText().toString();
                        search(context, mCondition);
                    }
                };
                return noDoubleClickListener;
            }
        });
        listView.setAdapter(adapter);
    }

    public interface SearchListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText condition);
    }
}
